﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ITM495.Lab2.Core;
using ITM495.Lab2.Core.Entities;

namespace ITM495.Lab2.Impl.Readers
{
    public class TextReader:IReader<Tweet>
    {
        private String _lineFormat = "{0}\t{1}\t{2}\t{3}\t{4}\r\n";

        protected String Path { get; set; }

        public TextReader(string path)
        {
            Path = path;
        }

        #region IReader<Tweet> Members

        public IList<Tweet> Read()
        {
            var tweets = new List<Tweet>();

            using(var textReader = new StreamReader(Path))
            {
                var line = textReader.ReadLine();

                while(line != null)
                {
                    var parts = line.Split(new[]{'\t'},StringSplitOptions.RemoveEmptyEntries);

                    var tweet = new Tweet()
                                    {
                                        Title = parts[0],
                                        Description = parts[1],
                                        PubDate = DateTime.Parse(parts[2]),
                                        Guid = parts[3],
                                        Link = parts[4]
                                    };

                    tweets.Add(tweet);

                    line = textReader.ReadLine();
                }
            }

            return tweets;
        }

        #endregion
    }
}
